//
//  SignToSelectWitnessView.swift
//  ezyform
//
//  Created by Moennig Q on 6/5/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
class SignToSelectWitnessView: UIView {
    private lazy var disposeBag = DisposeBag()
    var selectWitnessClosure: (()->Void)?
    let ownerAsWitnessBtn = WitnessButton(R.string.localizable.owner())
    let otherAsWitnessBtn = WitnessButton(R.string.localizable.other())
    var ownerAsWitnessFlag: Bool {
        if ownerAsWitnessBtn.tag == 1 {
            return true
        }
        return false
    }
    //for code
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpView()
    }
    //for storyboard
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUpView()
    }
    
    private func setUpView() {
        //tip for witness
        let tipForWitnessLabel = BaseLabel()
        tipForWitnessLabel.attributedText = self.getAttributedText("Please select your witness ", secStr: "(Must be onsite)")
        tipForWitnessLabel.numberOfLines = 0
        tipForWitnessLabel.lineBreakMode = .byWordWrapping
        self.addSubview(tipForWitnessLabel)
        tipForWitnessLabel.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
        }
        // owner witness btn
        self.addSubview(ownerAsWitnessBtn)
        ownerAsWitnessBtn.snp.makeConstraints { (make) in
            make.top.equalTo(tipForWitnessLabel.snp.bottom).offset(12)
            make.left.equalToSuperview()
            make.height.equalTo(34)
            make.bottom.equalToSuperview()
        }
        // other witness btn
        self.addSubview(otherAsWitnessBtn)
        otherAsWitnessBtn.snp.makeConstraints { (make) in
            make.top.equalTo(ownerAsWitnessBtn)
            make.left.equalTo(ownerAsWitnessBtn.snp.right).offset(12)
            make.height.width.equalTo(ownerAsWitnessBtn)
            make.right.equalToSuperview()
        }
        //监听owner按钮的点击事件
        ownerAsWitnessBtn.rx.tap.subscribe(onNext: {[weak self] _ in
            guard let strongSelf = self else {return }
            strongSelf.selectOwnerAsWitness()
        }).disposed(by: self.disposeBag)
        //监听other按钮的点击事件
        otherAsWitnessBtn.rx.tap.subscribe(onNext: {[weak self] _ in
            guard let strongSelf = self else {return }
            strongSelf.selectOtherAsWitness()
        }).disposed(by: self.disposeBag)
        //默认选择Owner
        selectOwnerAsWitness()
    }
    func selectOwnerAsWitness(){
        self.ownerAsWitnessBtn.selectBtn()
        self.otherAsWitnessBtn.unSelectBtn()
        self.selectWitnessClosure?()
    }
    func selectOtherAsWitness(){
        self.ownerAsWitnessBtn.unSelectBtn()
        self.otherAsWitnessBtn.selectBtn()
        self.selectWitnessClosure?()
    }
    fileprivate func getAttributedText(_ firstStr: String, secStr: String)->NSAttributedString{
        let combineStr = firstStr + secStr
        let tempStr = combineStr as NSString
        let localtion = tempStr.range(of: secStr)
        let attrString = NSMutableAttributedString(string: combineStr)
        let attr: [NSAttributedString.Key : Any] = [.font: R.font.interMedium(size: 14)!,.foregroundColor: UIColor(hexStr: "#292D32")]
        let attr2: [NSAttributedString.Key : Any] = [.font: R.font.interMedium(size: 14)!,.foregroundColor: UIColor(hexStr: "#ED3E3E")]
        
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        attrString.addAttributes(attr2, range: NSRange(location: localtion.location, length: localtion.length))
        return attrString
    }
}
class WitnessButton: BaseButton{
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initView()
    }
    init(_ title: String) {
        super.init(frame: CGRect.zero)
        self.initView()
        self.setTitle(title, for: UIControl.State.normal)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initView() {
        self.layer.cornerRadius = Constant.BackViewCornerRadius
        self.clipsToBounds = true
        self.setTitle("", for: UIControl.State.normal)
        self.setFont(R.font.interMedium(size: 17))
    }
    func selectBtn(){
        self.tag = 1
        self.backgroundColor = UIColor(hexStr: "#005D2D1A")
        self.setTitleColor(UIColor(hexStr: Constant.ThemeColorHexStr), for: UIControl.State.normal)
        self.layer.borderColor = UIColor(hexStr: Constant.ThemeColorHexStr).cgColor
        self.layer.borderWidth = 1
    }
    func unSelectBtn(){
        self.tag = 0
        self.backgroundColor = UIColor(hexStr: "#F5F5F5")
        self.setTitleColor(UIColor(hexStr: "#292D32"), for: UIControl.State.normal)
        self.layer.borderColor = UIColor(hexStr: Constant.TransparentColorHexStr).cgColor
        self.layer.borderWidth = 0
    }
}
